<template>
    <div>
        <el-row :gutter="15">
			<el-form ref="elForm" :model="dataForm" :rules="rules" size="medium" label-width="100px" label-position="right" :disabled="setting.readonly">
				<el-col :span="12" v-if="JudgeShow('number')">
					<el-form-item label="流程编号" prop="number" >
						<el-input v-model="dataForm.number"  :disabled ="JudgeWrite('number')"  placeholder="系统自动生成" readonly >
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="JudgeShow('psrq')">
					<el-form-item label="评审日期" prop="psrq" required >
						<el-date-picker v-model="dataForm.psrq"  :disabled ="JudgeWrite('psrq')"  placeholder="请选择" clearable :style='{"width":"100%"}' type="date" format="yyyy-MM-dd" value-format="timestamp" >
						</el-date-picker>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="JudgeShow('psr')">
					<el-form-item label="评审人" prop="psr" required >
						<el-select v-model="dataForm.psr"  :disabled ="JudgeWrite('psr')"  placeholder="请选择" clearable :style='{"width":"100%"}' >
							<el-option v-for="(item, index) in psrOptions" :key="index" :label="item.F_RealName" :value="item.F_Id" ></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="JudgeShow('cpry')">
					<el-form-item label="参评人员" prop="cpry" required >
						<el-select v-model="dataForm.cpry"  :disabled ="JudgeWrite('cpry')"  placeholder="请选择" clearable :style='{"width":"100%"}' >
							<el-option v-for="(item, index) in cpryOptions" :key="index" :label="item.F_RealName" :value="item.F_Id" ></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="JudgeShow('psxgfj')">
					<el-form-item label="评审相关附件" prop="psxgfj" >
						<JNPF-UploadFz v-model="psxgfjList"  :disabled ="JudgeWrite('psxgfj')"  accept="" :fileSize="10" sizeUnit="MB" :limit="1" buttonText="点击上传" >
						</JNPF-UploadFz>
					</el-form-item>
				</el-col>
				<el-col :span="24" v-if="JudgeShow('psyj')">
					<el-form-item label="评审意见" prop="psyj" required >
						<el-input v-model="dataForm.psyj"  :disabled ="JudgeWrite('psyj')"  placeholder="请输入" show-word-limit :style='{"width":"100%"}' type="textarea" :autosize='{"minRows":4,"maxRows":1000}' >
						</el-input>
					</el-form-item>
				</el-col>
				<el-col :span="12" v-if="JudgeShow('mainId')" style="display:none;">
					<el-form-item label="主表ID" prop="mainId" >
						<el-input v-model="dataForm.mainId" disabled  placeholder="请输入" clearable :style='{"width":"100%"}' >
							<template slot="prepend"></template>
							<template slot="append"></template>
						</el-input>
					</el-form-item>
				</el-col>
			</el-form>

        </el-row>
        <UserBox v-if="userBoxVisible" ref="userBox" @submit="submit" />
    </div>
</template>
<script>
    import request from '@/utils/request'
    import { previewDataInterface } from '@/api/systemData/dataInterface'
    import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
    export default {
        components: {},
        props: [],
        data() {
            return {
                visible: false,
                setting: {},
                eventType: '',
                userBoxVisible: false,
                dataForm: {
                    id:'',                    
					flowId: '',
					actionType:1,
					freeApproverUserId: '',
					number:undefined,
					psrq:undefined,
					psr:undefined,
					cpry:undefined,
					psxgfj:[],
					psyj:undefined,
					mainId:undefined,

                },
                rules: {
						psrq:[
						{
							required:true,
							message:'请输入评审日期',
							trigger:'blur'
						},
						],
						psr:[
						{
							required:true,
							message:'请输入评审人',
							trigger:'blur'
						},
						],
						cpry:[
						{
							required:true,
							message:'请输入参评人员',
							trigger:'blur'
						},
						],
						psyj:[
						{
							required:true,
							message:'请输入评审意见',
							trigger:'blur'
						},
						],

                },
				psrOptions:[],
				cpryOptions:[],

					psxgfjList:[],

            }
        },
        computed: {},
        watch: {},
        created() {
            this.getpsrOptions();
            this.getcpryOptions();

        },
        mounted() { },
        methods: {
            getpsrOptions()
            {
                previewDataInterface('8bca424f16d6448ab09e57a028853aa9').then(res => {
                    this.psrOptions = res.data
                })
            },
            getcpryOptions()
            {
                previewDataInterface('8bca424f16d6448ab09e57a028853aa9').then(res => {
                    this.cpryOptions = res.data
                })
            },

            goBack() {
                this.$emit('refresh')
            },
            init(data) {
                this.loading = true
                this.dataForm.id = data.id || ''
                this.setting = data
                this.visible = true;
                this.$nextTick(() => {
                    this.$refs['elForm'].resetFields();
                    if (this.dataForm.id) {
                        request({
                            url: '/api/WorkFlowForm/qyscAqgzzds/' + this.dataForm.id,
                            method: 'get'
                        }).then(res => {
                            this.dataForm = res.data
				            this.psxgfjList = this.dataForm.psxgfj ? JSON.parse(this.dataForm.psxgfj) : [];

                        })
                    } else {
                        this.dataForm.flowId = data.flowId
                        this.dataForm.mainId = data.mainid; 
                    }
                    this.loading = false
                })
            },
            // 表单提交
            dataFormSubmit(eventType) {            
				this.$set(this.dataForm, 'psxgfj', JSON.stringify(this.psxgfjList));

                this.$refs['elForm'].validate((valid) => {
                    if (valid) {
                        if (eventType === 'audit' || eventType === 'reject') {
                            this.$emit('approval', this.dataForm, eventType)
                            return
                         }
                        this.dataForm.actionType = eventType === 'submit' ? 0 : 1
                        this.eventType = eventType
                        if (this.eventType === 'submit') {
                            if (this.setting.freeApprover == 0) {
                                this.$confirm('您确定要提交当前流程吗, 是否继续?', '提示', {
                                    type: 'warning'
                                }).then(() => {
                                    this.submit()
                                }).catch(() => { });
                            } else {
                                this.userBoxVisible = true
                                this.$nextTick(() => {
                                    this.$refs.userBox.init()
                                })
                            }
                        } else {
                            this.request()
                        }
                    }
                })
            },
            submit(freeApproverUserId) {
                if (freeApproverUserId) this.dataForm.freeApproverUserId = freeApproverUserId
                this.request()
            },
            request() {
                if (!this.dataForm.id) {
                    delete (this.dataForm.id)
                    request({
                        url: '/api/WorkFlowForm/qyscAqgzzds',
                        method: 'post',
                        data: this.dataForm
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.$emit('close', true)
                            }
                        })
                    })
                } else {
                    request({
                        url: '/api/WorkFlowForm/qyscAqgzzds/' + this.dataForm.id,
                        method: 'PUT',
                        data: this.dataForm
                    }).then((res) => {
                        this.$message({
                            message: res.msg,
                            type: 'success',
                            duration: 1000,
                            onClose: () => {
                                this.visible = false
                                this.$emit('close', true)
                            }
                        })
                    })
                }
            },
            JudgeShow(id) {
                if (!this.setting.formOperates || !this.setting.formOperates.length) return true
                let arr = this.setting.formOperates.filter(o => o.id === id) || []
                if (!arr.length) return true
                let item = arr[0]
                return item.read
            },
            JudgeWrite(id) {
                if (!this.setting.formOperates || !this.setting.formOperates.length) return false
                let arr = this.setting.formOperates.filter(o => o.id === id) || []
                if (!arr.length) return true
                let item = arr[0]
                return !item.write
            },

        },
    }

</script>
